//
//  FFPayOrderView.m
//  FFMall
//
//  Created by ale tan on 2017/9/21.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "FFPayOrderView.h"

#import "FFPayOrderChooseAddressView.h"
#import "FFPayOrderChooseShippingWayView.h"
#import "FFPayOrderPriceView.h"
#import "FFPayOrderPriceView.h"

//请求
#import "GetAddressRequest.h"
#import "GetOrderRequest.h"

@interface FFPayOrderView ()

@property (nonatomic ,strong) FBOrderModel *model;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *deliveryLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

@property (nonatomic ,strong) NSMutableArray *addresses;
@property (nonatomic ,strong) FBAddressModel *selectedAddress;

@property (nonatomic ,strong) FBShippingWayModel *selectedShippingWay;

@property (nonatomic ,strong) NSDictionary *infos;

@property (nonatomic ,strong) PayOrder payOrderBlock;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *toBottom;//底部距离，适配iPhone X

@end

@implementation FFPayOrderView

+ (instancetype)orderViewWithInfos:(NSDictionary *)infos Model:(FBOrderModel *)orderModel{
    FFPayOrderView *orderView = [[[NSBundle mainBundle] loadNibNamed:@"FFPayOrderView" owner:nil options:nil] firstObject];
    orderView.model = orderModel;
    orderView.infos = infos;
    if (orderModel.address) {
        orderView.selectedAddress = orderModel.address;
    }
    if (orderModel.shipping) {
        orderView.selectedShippingWay = orderModel.shipping;
    }
    
    return orderView;
}

- (void)show{
    UIView *view = [UIApplication sharedApplication].keyWindow.rootViewController.view;//[[UIApplication sharedApplication].delegate window];
    self.frame = view.bounds;
    [self layoutIfNeeded];
    [self updateDatas];
    [view addSubview:self];
    
    GetAddressRequest *request = [GetAddressRequest new];
    [request getAddressListWithComplete:^(id responseDic, BOOL result) {
        if (result) {
            NSArray *addresses = (NSArray *)responseDic;
            self.addresses = [NSMutableArray arrayWithArray:addresses];
        }
    }];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    _toBottom.constant = isDeviceIphoneX?34:0;
}

- (void)setPayOrder:(PayOrder)payOrder{
    _payOrderBlock = payOrder;
}

- (void)updateDatas{
    if (_selectedAddress) {
        _addressLabel.text = _selectedAddress.address;
    }
    else{
        _addressLabel.text = @"请选择地址";
    }
    
    //配送方式
    _deliveryLabel.text = _selectedShippingWay.shippingTitle;
    
    //价格
    NSString *currencyLogo = @"¥";
//    if (_model.prices[@"currencyLogo"]) {
//        currencyLogo = _model.prices[@"currencyLogo"];
//    }
    _priceLabel.text = [NSString stringWithFormat:@"%@%.2f（含运费）",currencyLogo,[_model.prices[@"amountPayable"] floatValue]];
}

#pragma mark -Events-
//查看价格
- (IBAction)clickToShowPrices:(UIButton *)sender {
    FFPayOrderPriceView *priceView = [FFPayOrderPriceView priceViewWithPrices:_model.prices SelectedShippingWay:_selectedShippingWay];
    
    self.hidden = YES;
    WS(weakSelf);
    [priceView setConfirmPayOrder:^(BOOL confirm) {
        self.hidden = NO;
        if (confirm) {
            [weakSelf clickToPayOrder:sender];
        }
    }];
    [priceView show];
}

//选择配送方式
- (IBAction)clickToChooseShippingWay:(UIButton *)sender {
    FFPayOrderChooseShippingWayView *shippingWayView = [FFPayOrderChooseShippingWayView chooseShippingWayWithWays:@[_model.shipping] SelectedWay:_selectedShippingWay];
    self.hidden = YES;
    
    WS(weakSelf);
    [shippingWayView setChooseShippingWay:^(FBShippingWayModel *model) {
        self.hidden = NO;
        if (model && ![model.shippingId isEqual:weakSelf.selectedShippingWay.shippingId]) {
            weakSelf.selectedShippingWay = model;
            [weakSelf updateDatas];
        }
    }];
    [shippingWayView show];
}

//选择收货地址
- (IBAction)clickToChooseAddress:(UIButton *)sender {
    if (_addresses.count) {
        [self showAddressView];
    }
    else{
        GetAddressRequest *request = [GetAddressRequest new];
        [request getAddressListWithComplete:^(id responseDic, BOOL result) {
            if (result) {
                NSArray *addresses = (NSArray *)responseDic;
                self.addresses = [NSMutableArray arrayWithArray:addresses];
                [self showAddressView];
            }
        }];
    }
}

//取消下单
- (IBAction)clickToCanclePayOrder:(id)sender {
    [self removeFromSuperview];
}

//显示选择地址页
- (void)showAddressView{
    FFPayOrderChooseAddressView *addressView = [FFPayOrderChooseAddressView chooseAddressViewWithAddresses:self.addresses selectedAddress:self.selectedAddress];
    self.hidden = YES;
    
    [addressView setChooseAddress:^(FBAddressModel *chooseModel) {
        self.hidden = NO;
        if (chooseModel) {
            self.selectedAddress = chooseModel;
            
            [self updateDatas];
        }
    }];
    [addressView show];
}

//下单
- (IBAction)clickToPayOrder:(UIButton *)sender {

    if (!_selectedAddress) {
        [FFTipsView showTips:@"请选择地址"];
        return;
    }

    GetOrderRequest *request = [GetOrderRequest new];
    _model.address = _selectedAddress;
    _model.shipping = _selectedShippingWay;
    [request createOrderWithModel:_model Complete:^(id responseDic, BOOL result) {
        if (result) {
            [self removeFromSuperview];
            FBOrderModel *order = (FBOrderModel *)responseDic;
            if (_payOrderBlock) {
                _payOrderBlock(order,YES);
            }
        }
    }];
    
}
@end
